﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using WidgetsDesign.API.DataBase;
using WidgetsDesign.API.Models;

namespace WidgetsDesign.API.Services
{
    public class RechargeRepository:IRechargeRepository
    {
        private readonly AppDbContext _context;
        public RechargeRepository(AppDbContext content)
        {
            _context = content;
        }
        public void CreateRechargeCode(RechargeCard rechargeCard)
        {
            _context.RechargeCard.Add(rechargeCard);
            _context.SaveChanges();
        }

        public async Task<RechargeCard?> GetRechargeCode(string key)
        {
          return await _context.RechargeCard.FirstOrDefaultAsync(x => x.Key == key);
        }

        public async Task<bool> ExistUseRechargeCode(string key)
        {
            return await _context.RechargeRecord.AnyAsync(x => x.Key == key);
        }

        public void Conversion(RechargeRecord rechargeRecord)
        {
            _context.RechargeRecord.Add(rechargeRecord);
            _context.SaveChanges();
        }
    }
}
